package com.wangshidai.controller;

import com.wangshidai.entity.R;
import com.wangshidai.form.rolePermission.AlterRolePermissionFrom;
import com.wangshidai.service.SysRolePermissionService;
import com.wangshidai.util.BusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@Api(tags = "角色权限操作接口")
@RequestMapping("rolePermission")
public class RolePermissionController {
    @Autowired
    private SysRolePermissionService sysRolePermissionService;
    @GetMapping("/queryPermissionId/{roleId}")
    @ApiOperation("根据id查询指定角色权限")
    public R queryRolePermissionId(@PathVariable @ApiParam("角色id") long roleId){
        log.info("接收到前端查询角色权限请求，参数{}",roleId);
        List<Long> permissionIds = sysRolePermissionService.queryPermissionId(roleId);
        if(permissionIds.isEmpty()){
            return new R(-200,"未查询到权限",null);
        }
        return new R(200,"查询成功",permissionIds);
    }
    @PutMapping("/alterRolePermission")
    @ApiOperation("修改角色权限")
    public R alterRolePermission(@Valid @RequestBody AlterRolePermissionFrom alterRolePermission) throws BusinessException {
        log.info("接收到前端给角色主键{}分配权限的请求，权限主键{}",alterRolePermission.getRoleId(),alterRolePermission.getPermissionIds());
        long roleId = alterRolePermission.getRoleId();
        Set<Long> permissionIds = alterRolePermission.getPermissionIds();
        boolean flog = sysRolePermissionService.alterRolePermission(roleId, permissionIds);
        if(flog){
            return new R(200,"分配成功",null);
        }
        return new R(-200,"分配失败",null);
    }
}